﻿namespace HomeworkOppPartOne
{
    using System;
    using System.Collections.Generic;
    
    class MobilePhone
    {
        static public MobilePhone iPhone4s = new MobilePhone("iPhone4s", "Apple");

        private string Model {get; set;}
        private string Manufacturer {get; set;}
        private decimal price;
        private string Owner {get; set;}
        private Battery phoneBattery {get; set;}
        private Display phoneDisplay {get; set;}
        private List<Call> callHistory;

        internal MobilePhone()
        {
            this.Model = Model;
            this.Manufacturer = Manufacturer;
            this.Price = price;
            this.Owner = Owner;
            this.phoneBattery = phoneBattery;
            this.phoneDisplay = phoneDisplay;
            this.CallHistory = new List<Call>();
        }
        
        internal MobilePhone(string model): this()
        {
        }
        
        internal MobilePhone(string model,string manufacturer)
            : this()
        {
        }

        internal MobilePhone(string model,string manufacturer,decimal price)
            : this()
        {
        }
        
        internal MobilePhone(string model,string manufacturer,decimal price,string owner)
            : this()
        {
        }

        internal MobilePhone(string model,string manufacturer,decimal price,string owner,Battery phoneBattery)
            : this()
        {
        }

        internal MobilePhone(string model,string manufacturer,decimal price,string owner,Battery phoneBattery, Display phoneDisplay)
            : this()
        {
        }

        internal MobilePhone(string model,string manufacturer,decimal price,string owner,Battery phoneBattery,List<Call> callHistory)
            : this()
        {
        }
        
        internal decimal Price
        {
            get{return this.price;}
            private set
            {
                if (value < 0)
	            {
		            throw new ArgumentException("Invalid Input! Price can`t be negative value");
	            }
                this.price = value;
            }
        }

        internal List<Call> CallHistory
        {
            get { return this.callHistory; }
            set { this.callHistory = value; }
        }

        internal void IncommingCall(string callerId,double callDuration)
        {
            Call newCall = new Call(callerId, callDuration);
            CallHistory.Add(newCall);
        }

        internal void DeleteCall(int index)
        {
            if ((this.callHistory.Count == 0) || ((this.callHistory.Count - 1) < index))
            {
                throw new ArgumentException("Invalid Input! There is no call to delete!");
            }
            callHistory.RemoveAt(index);
        }

        internal decimal CurrentFacture(decimal pricePerMinute)
        {
            double currentDurationSpend = 0;
            foreach (var duration in callHistory)
            {
                currentDurationSpend += duration.CallDuration;
            }
            return ((decimal) Math.Round((currentDurationSpend / 61)) * pricePerMinute);
        }

        internal void LongestCallRemove()
        {
            int currentIndex = 0;
            double longestDuration = 0;
            for (int i = 0; i < callHistory.Count; i++)
            {
                if (this.callHistory[i].CallDuration > longestDuration)
                {
                    longestDuration = this.callHistory[i].CallDuration;
                    currentIndex = i;
                }
                DeleteCall(currentIndex);
            }
        }

        internal void ClearHistory()
        {
            callHistory.Clear();
        }

        public override string ToString()
        {
            return string.Format("Model: {0}\nManufacturer:\n{1}\nPrice: {2}\nOwner: {3}\n{4}\n{5}",
            this.Model,this.Manufacturer,this.Price,this.phoneBattery,this.phoneDisplay);
        }
    }
}